package luj.tool.jnproxyan.proxy.util.data.wrap;

import luj.data.api.Data;
import luj.data.api.use.UseGroup;
import luj.tool.jnproxyan.proxy.database.DatabaseState;
import luj.tool.jnproxyan.proxy.util.data.load.DataByIdLoader;
import luj.tool.jnproxyan.proxy.util.data.session.DataUsingStarter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Map;

@Component
public class ResultByIdLWrapper {

  public ResultByIdLWrapper load(Long dataId, DataUsingStarter.Session session) {
    return load(dataId, session.db(), session.loaded(), session.using());
  }

  public ResultByIdLWrapper load(Long dataId, DatabaseState dbState, Map<String, Data> loadCache, UseGroup useGroup) {
    ResultByIdLWrapper result = new ResultByIdLWrapper();
    result._dataId = dataId;
    result._dbState = dbState;
    result._loadCache = loadCache;
    result._useGroup = useGroup;

    result._dataLoader = _dataLoader;
    result._dataWrapper = _dataWrapper;

    return result;
  }

  // loadAndWrap
  public <T> T andWrap(Class<T> clazz) {
    Data data = _dataLoader.inject(clazz, _dataId, _dbState, _loadCache).load();

    return _dataWrapper.inject(data, _loadCache, _useGroup).wrap(clazz);
  }

  Long _dataId;

  DatabaseState _dbState;
  Map<String, Data> _loadCache;
  UseGroup _useGroup;

  @Autowired
  DataByIdLoader _dataLoader;

  @Autowired
  DataResultWrapper _dataWrapper;
}
